package main

import (
	"fmt"
	greetings "husd.com/framework/v0/greetings"
	"husd.com/framework/v0/proto_demo"
	"io/ioutil"
	"net/http"
	"os"
	"time"
)

func main() {

	proto_demo.ProtoDemo()
}

func Validation() []error {
	var errs []error

	//append(errs, errors.New("error 1"))

	return errs
}

func _() {

	//Validation()
	f := "../resource/app.properties"
	greetings.PropertiesConfigDemo(f)
}

func a122() {

	i, j := 0, 0
	if true {
		j, k := 1, 1
		fmt.Printf("j = %d, k = %d\n", j, k)
	}
	fmt.Printf("i = %d, j = %d\n", i, j)
}

func _() {

	bytes := greetings.Struct2JsonFormat()
	greetings.Json2Struct(bytes)
	s := "Hello World"
	fmt.Println(s)
	//greetings.TickDemo()
	//runtime.GOMAXPROCS(10)

	for {
		go get()
		time.Sleep(200 * time.Millisecond)
	}
}

func get() {
	fmt.Println("get url...................")
	resp, err := http.Get("http://localhost:8080/comment/set?click_url=123&click_app=1234&comment_msg=abcdef")
	b, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		fmt.Fprintf(os.Stderr, "fetch: reading: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("%s", string(b))
}

func _() {

	//go tcp.StartTcpServer()
	//go tcp.ConnectTcpServer()
	//greetings.StartHttpPingServer(8080)

	m := make(map[string]int)
	m["abc"]++

	fmt.Println("abc value:", m["abc"])

	p := new(int)
	q := new(int)

	// false 地址不同
	// true 地址上存的值是相同的
	fmt.Println(p == q)
	fmt.Println(*p == *q)

	test123()

	x := 10
	y := 20

	x, y = y, x
	fmt.Println("x ", x)
	fmt.Println("y ", y)

}

func test123() {

	int := "12345"
	fmt.Println("int is ", int)
}

type Test struct {
	name string
}

func (t *Test) Close() {
	fmt.Println(t.name, " closed")
}
func ErrorDeferDemo() {
	ts := []Test{{"a"}, {"b"}, {"c"}}
	for _, t := range ts {
		defer t.Close()
	}
}

func _() {

	//greetings.AnalysisEmailBody()
	//greetings.HelloAndGoToSchool("小明")
	//fmt.Println(msg)
	//mapDemo()
	//greetings.CreateArray()
	//greetings.SliceDemo()
	greetings.AnonymousFuncDemo()
	b := greetings.ClosePackage()
	b()
	b()
	b()

	greetings.ClosePackage()

	f := greetings.Factorial(10)
	fmt.Println("递归：", f)

	//greetings.PanicDemo()
	greetings.ErrorDeferDemo()

	greetings.ErrorDeferDemo2()
}

func mapDemo() {
	//Map 结构
	mapDemo := greetings.CreateMapDemo()
	fmt.Println(mapDemo["1"])
	fmt.Println(mapDemo["4"])

	//判断是否存在 简写，也可以分成2行写
	if _, ok := mapDemo["12345"]; !ok {
		fmt.Println("12345 不存在")
	}

	//遍历map
	for k, v := range mapDemo {
		fmt.Println("k is :", k, "value is :", v)
	}

	//删除某个元素
	delete(mapDemo, "1")

	fmt.Println("删除之后。。。。。。")

	//遍历map
	for k, v := range mapDemo {
		fmt.Println("k is :", k, "value is :", v)
	}

	fmt.Println("清空之后。。。。。。")
	//清空 重新make一个
	mapDemo = make(map[string]string)

	//遍历map
	for k, v := range mapDemo {
		fmt.Println("k is :", k, "value is :", v)
	}
}

func _() {
	// print hello world
	fmt.Println("hello world this is the first go code")
	//fmt.Println(quote.Go())
	var a int8
	a = 104
	//b := 12
	fmt.Println("this is a ", a)

	//random
	fmt.Println(fmt.Sprintf("演示随时数字：%d ", greetings.RandomInt()))

	//Map 结构
	mapDemo := greetings.CreateMapDemo()
	fmt.Println(mapDemo["1"])
	fmt.Println(mapDemo["4"])

	//判断是否存在 简写，也可以分成2行写
	if _, ok := mapDemo["12345"]; !ok {
		fmt.Println("12345 不存在")
	}

	//遍历map
	for k, v := range mapDemo {
		fmt.Println("k is :", k, "value is :", v)
	}

	//数组
	arrDemo := []string{"1", "2", "3"}
	for _, item := range arrDemo {
		fmt.Println(item)
	}

	//链表
	greetings.ListIntDemo()
	greetings.ListStringDemo()

	p := greetings.GetCurrentPath()
	fmt.Println("当前文件目录:", p)
}
